import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_slider_drawer/flutter_slider_drawer.dart';
import 'package:sptapp/moudle/event_bus.dart';
import 'package:sptapp/page/index_page.dart';

import 'home/home.dart';
import 'min/min.dart';

// final GlobalKey<SliderDrawerState> drawerKey = GlobalKey<SliderDrawerState>();


class DrawerIndexPage extends StatefulWidget {
  const DrawerIndexPage({super.key});

  @override
  State<DrawerIndexPage> createState() => _DrawerIndexPageState();
}

class _DrawerIndexPageState extends State<DrawerIndexPage> {

  bool isOpen = false;
  final GlobalKey<SliderDrawerState> drawerKey = GlobalKey<SliderDrawerState>();
  late StreamSubscription _subscription;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _subscription = eventBus.on<IndexSliderChange>().listen((val){
      setState(() {
        isOpen = val.isOpen;
      });
      if(isOpen){
        drawerKey.currentState?.openSlider();
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _subscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return SliderDrawer(
      key: drawerKey,
      sliderOpenSize: 520.w,
      isDraggable: false,
      splashColor: Colors.transparent,
      appBar: Container(),
      slider: MinPage(),
      child: Stack(
        children: [
          IndexPage(),
          Positioned(child: Visibility(
            visible: isOpen,
            child: GestureDetector(
              onTap: (){
                drawerKey.currentState?.closeSlider();
                setState(() {
                  isOpen = false;
                });
                eventBus.fire(IndexSliderChange(isOpen));
              },
              child: Container(
                width: 1.sw,
                height: 1.sh,
                color: Color(0x5f000000),
              ),
            ),
          ),)
        ],
      ),
    );
  }
}
